package com.rpay.admin.controller;

import com.rpay.admin.config.jwt.AuthTokenDetails;
import com.rpay.admin.config.jwt.JsonWebTokenUtility;
import com.rpay.common.enums.PcsResultCode;
import com.rpay.common.util.common.CommonUtils;
import com.rpay.common.vo.PcsResult;
import com.rpay.dao.mapper.UserTradeDOMapper;
import com.rpay.dao.user.UserTradeDao;
import com.rpay.model.admin.RpayAdminModule;
import com.rpay.model.admin.RpayAdminUser;
import com.rpay.model.user.UserTradeDO;
import com.rpay.service.admin.impl.ModuleServiceImpl;
import com.rpay.service.admin.impl.SystemUserServiceImpl;
import com.rpay.service.user.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Title：
 * Description
 *
 * @author fxg06 on 2018/5/7
 * @version 1.0
 */
@RestController
@RequestMapping(value = "noa")
public class NoaController {
    @Autowired
    private UserTradeDao userTradeDao;

    @Autowired
    private IUserService iUserService;

    @Autowired
    private SystemUserServiceImpl systemUserService;
    @Autowired
    private ModuleServiceImpl moduleServiceImpl;

    private JsonWebTokenUtility tokenService = new JsonWebTokenUtility();

    @Autowired
    private UserTradeDOMapper userTradeDOMapper;


    /**
     * pc端登录接口
     *
     * @return 登录结果
     */
    @PostMapping(value = "/login")
    public PcsResult loginPC(@RequestBody Map paramUser) {
        String username = paramUser.get("username").toString();
        String password = paramUser.get("password").toString();
        RpayAdminUser inputUser = new RpayAdminUser();
        inputUser.setUsername(username);
        inputUser.setPassword(password);
        RpayAdminUser userR = systemUserService.findByUser(inputUser);
        if (userR != null) {
            Map result = new HashMap();
            //set userinfo need return
            Map user = new HashMap();
            user.put("realName", userR.getRealName());
            user.put("status", userR.getStatus());
            user.put("userType", userR.getUserType());
            user.put("username", userR.getUsername());
            result.put("user", user);

            //set permission list
            List<RpayAdminModule> modulelist = moduleServiceImpl.listModuleByUserId(userR.getId());
            if (userR.getStatus() == 1) {//用户未被冻结返回所有权限

                result.put("modulelist", modulelist);

            } else {
                //用户被冻结，只返回订单和账户中心（如果用户有此权限的话）

                return new PcsResult(PcsResultCode.USER_INVALID);
            }

            //set token
            result.put("token", createToken(userR));
            return PcsResult.build(result);
        } else {
            return PcsResult.build(20202, "用户名密码错误");
        }
    }

    /**
     * 构造token
     *
     * @param userR 用户信息
     * @return token
     */
    private String createToken(RpayAdminUser userR) {
        AuthTokenDetails authTokenDetails = new AuthTokenDetails();
        authTokenDetails.setId(userR.getId());
        authTokenDetails.setUsername(userR.getUsername());
        //todo token 过期的时候给前端提示
        authTokenDetails.setExpirationDate(CommonUtils.buildExpirationDate());
        // Create auth token
        String jwt = tokenService.createJsonWebToken(authTokenDetails);
        return jwt;
    }

    @GetMapping("/test")
    public void test1() {
        UserTradeDO userTradeDO = new UserTradeDO();
        userTradeDO.setUserId(3996);
        userTradeDO.setCost(new BigDecimal("1"));
        userTradeDO.setRippleAmount(new BigDecimal("9"));
        userTradeDO.setExpiry_date("2018-06-11 00:00:00");
        userTradeDO.setType("WITHDRAW");
        userTradeDO.setStatus("VERIFY");
        userTradeDO.setDestination_wallet_id(4);
        userTradeDO.setWithdraw_wallet_id(1);
        userTradeDO.setRate(new BigDecimal("0.1"));
        System.out.println(userTradeDO);
        userTradeDao.insert(userTradeDO);
    }

}
